package com.tanhua.autoconfig.template;

import com.aliyun.imageaudit20191230.Client;
import com.aliyun.imageaudit20191230.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class AliYunGreenTemplate {

    @Autowired
    private Client client;

    @Autowired
    private ScanTextRequest scanTextRequest;

    @Autowired
    private ScanImageRequest scanImageRequest;

    public String ScanText(String scanText) {
        log.info("文本检测: {}", scanText);
        ScanTextRequest.ScanTextRequestTasks tasks0 = new ScanTextRequest.ScanTextRequestTasks()
                .setContent(scanText);
        scanTextRequest.setTasks(Arrays.asList(tasks0));
        // 复制代码运行请自行打印 API 的返回值
        ScanTextResponse scanTextResponse = null;
        try {
            scanTextResponse = client.scanText(scanTextRequest);
        } catch (Exception e) {
            throw new RuntimeException("aliyun文本检测异常");
        }
        String code = scanTextResponse.getHeaders().get("code");
        log.info("code ? {}", code);
        List<ScanTextResponseBody.ScanTextResponseBodyDataElementsResults> results = scanTextResponse.body.data.elements.get(0).results;
        int flag = 0;
        for (ScanTextResponseBody.ScanTextResponseBodyDataElementsResults result : results) {
            String suggestion = result.getSuggestion();
            if ((suggestion.equals("block"))) {
                return suggestion;
            }
            if ((suggestion.equals("review"))) {
                flag++;
            }
        }
        return flag == 0 ? "pass" : "review";
    }

    public String ScanImages(List<String> urls) {
        ArrayList<ScanImageRequest.ScanImageRequestTask> scanImageRequestTasks = new ArrayList<>();
        for (String url : urls) {
            ScanImageRequest.ScanImageRequestTask task = new ScanImageRequest.ScanImageRequestTask()
                    .setImageURL(url);
            scanImageRequestTasks.add(task);
        }
        scanImageRequest.setTask(scanImageRequestTasks);
        ScanImageResponse scanImageResponse = null;
        try {
            scanImageResponse = client.scanImage(scanImageRequest);
        } catch (Exception e) {
            throw new RuntimeException("aliyun检测图片异常");
        }
        List<ScanImageResponseBody.ScanImageResponseBodyDataResults> results = scanImageResponse.body.data.results;
        int flag = 0;
        for (ScanImageResponseBody.ScanImageResponseBodyDataResults result : results) {
            List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = result.subResults;
            for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults subResult : subResults) {
                String suggestion = subResult.getSuggestion();
                if ((suggestion.equals("block"))) {
                    return suggestion;
                }
                if ((suggestion.equals("review"))) {
                    flag++;
                }
            }
        }
        return flag == 0 ? "pass" : "review";
    }
}
